package com.dream.model.app;

import java.io.Serializable;
import java.util.Map;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.Type;
import org.springframework.util.StringUtils;

import com.dream.model.base.AppUserModelBase;
import com.google.common.collect.Maps;

@Entity
@Table(name="t_app_position")
public class AppPosition extends AppUserModelBase implements Serializable {
	

	/**
	 * 
	 */
	private static final long serialVersionUID = 606278834180485395L;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;
	
	@Column(name="name")
	private String name;
	
	@Column(name="deleted")
	@Type(type = "org.hibernate.type.NumericBooleanType")
	private boolean deleted;
	
	
	@Column(name="type")
	@Enumerated(EnumType.STRING)
	private PositionType type;
	
	public PositionType getType() {
		return type;
	}

	public void setType(PositionType type) {
		this.type = type;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public boolean isDeleted() {
		return deleted;
	}

	public void setDeleted(boolean deleted) {
		this.deleted = deleted;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		AppPosition other = (AppPosition) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}
	
	public static enum PositionType{
		manager,staff;
		
		private static Map<String,PositionType> map = Maps.newConcurrentMap();
		static{
			for(PositionType type : values()){
				map.put(type.name(), type);
			}
		}
		
		public static PositionType instance(String name){
			
			if(StringUtils.isEmpty(name) || !map.containsKey(name)){
				return null;
			}
			return map.get(name);
		
		}
	}
}
